package com.spider.moudle.entity;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.Header;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 夏茂轩
 * @description 回复实体
 * @date 2017/11/8 9:21
 */
public class ResponseEntity {
    private String url;
    private String response_body;
    private int status_code;
    private ArrayList<Header> headers;
    private String encoding;
    private long response_time;
    private String proxy;
    private byte[] bytes;

    public String getProxy() {
        return proxy;
    }

    public void setProxy(String proxy) {
        this.proxy = proxy;
    }

    public Document getDocument() throws Exception {
        if (response_body != null) {
            return Jsoup.parse(response_body);
        } else {
            return null;
        }
    }
    public byte[] getBytes(){
        return bytes;
    }

    public String getEncoding() {
        return encoding;
    }

    public void setEncoding(String encoding) {
        this.encoding = encoding;
    }

    public ResponseEntity(String url,
                          String response_body, int status_code, ArrayList<Header> headers, String encoding, long response_time) {
        this.url = url;
        this.response_body = response_body;
        this.status_code = status_code;
        this.headers = headers;
        this.encoding = encoding;
        this.response_time = response_time;
    }

    public ResponseEntity(String url, String response_body, int status_code, ArrayList<Header> headers, String encoding, long response_time, String proxy) {
        this.url = url;
        this.response_body = response_body;
        this.status_code = status_code;
        this.headers = headers;
        this.encoding = encoding;
        this.response_time = response_time;
        this.proxy = proxy;
    }

    public ResponseEntity(String url, int status_code, ArrayList<Header> headers, long response_time, String proxy, byte[] bytes) {
        this.url = url;
        this.status_code = status_code;
        this.headers = headers;
        this.response_time = response_time;
        this.proxy = proxy;
        this.bytes = bytes;
    }

    public long getResponse_time() {
        return response_time;
    }

    public void setResponse_time(long response_time) {
        this.response_time = response_time;
    }

    public JSONObject getJSONObject() throws Exception {
        if (response_body != null) {
            return JSONObject.parseObject(response_body);
        } else {
            return null;
        }
    }

    public void setBytes(byte[] bytes) {
        this.bytes = bytes;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getResponse_body() {
        return response_body;
    }

    public void setResponse_body(String response_body) {
        this.response_body = response_body;
    }

    public int getStatus_code() {
        return status_code;
    }

    public void setStatus_code(int status_code) {
        this.status_code = status_code;
    }

    public ArrayList<Header> getHeaders() {
        return headers;
    }

    public void setHeaders(ArrayList<Header> headers) {
        this.headers = headers;
    }

    @Override
    public String toString() {
        JSONObject object = new JSONObject();
        int kb=(response_body.getBytes().length)/1024;
        object.put("url", url);
        object.put("status_code", status_code);
        object.put("response_time", response_time);
        object.put("encoding", encoding);
        object.put("html_size", kb+"kb");
        if (proxy != null) {
            object.put("proxy", proxy);
        }
        return object.toString();
    }
}
